package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.friend.entity.ContactsFriendEntity;
import com.forlove.app.qselect.friend.service.IInviteContactsService;
import com.forlove.app.qselect.friend.service.impl.InviteContactsServiceImpl;
import com.forlove.app.qselect.friend.view.InviteContactsView;

import java.util.HashMap;

/**
 * Created by yjl on 2017/11/17.
 */

public class InviteContactsPresenter extends IBasePresenter<IInviteContactsService> {
    private InviteContactsView inviteContactsView;
    private IInviteContactsService inviteContactsService;

    public InviteContactsPresenter(InviteContactsView inviteContactsView) {
        this.inviteContactsView = inviteContactsView;
        if (inviteContactsService == null) {
            inviteContactsService = new InviteContactsServiceImpl();
            setService(inviteContactsService);
        }
    }

    public void loadContactFriend() {
        inviteContactsService.loadData(ServerManager.ACTION_GET_CONTACTS_FRIEND, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                ContactsFriendEntity friendListEntity = new ContactsFriendEntity();
                friendListEntity.parseJSONString(ParseHelper.getString(result, "data"));
                inviteContactsView.updateFriendListView(friendListEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    inviteContactsView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    inviteContactsView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public void loadResetQuestionData() {
        inviteContactsService.loadData(ServerManager.ACTION_RESET_QUESTION, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "重置答题:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                AnswerEntity answerEntity = ParseHelper.getModel(result, "data", AnswerEntity.class);
                inviteContactsView.showResetAnswerView(answerEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (inviteContactsView != null) {
                        inviteContactsView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (inviteContactsView != null) {
                        inviteContactsView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }
}
